
#include "member_BTableItem.h"
#include "member_BTableWidget.h"
#include "member_BWidget.h"
#include "member_BObject.h"

using namespace BWE;

#define member					(*(member_BTableItem*)_ptr)
#define member_allocate()		_ptr = new member_BTableItem(this)
#define member_release()		delete (member_BTableItem*)_ptr

BTableItem::BTableItem()
{
	member_allocate();
}
BTableItem::BTableItem(const BString& text, Form form)
{
	member_allocate();
	setText(text);
	setForm(form);
}
BTableItem::BTableItem(Graph graph, const BString& text, Form form)
{
	member_allocate();
	setGraph(graph);
	setText(text);
	setForm(form);
}
BTableItem::BTableItem(BIcon* icon, const BString& text, Form form)
{
	member_allocate();
	setIcon(icon);
	setText(text);
	setForm(form);
}
BTableItem::BTableItem(BImage* image, const BString& text, Form form)
{
	member_allocate();
	setImage(image);
	setText(text);
	setForm(form);
}
BTableItem::BTableItem(BWidget* widget, const BString& text, Form form)
{
	member_allocate();
	setWidget(widget);
	setText(text);
	setForm(form);
}
BTableItem::~BTableItem()
{
	member_release();
}

bool BTableItem::setVisible(bool visible)
{
	if (member.visible != visible)
	{
		member.visible = visible;
		if (member.visible)
			this->emit(Signal_Show, true);
		else
			this->emit(Signal_Hide, false);
		member.dirtyCell();
		return true;
	}
	return false;
}
bool BTableItem::visible() const
{
	return member.visible;
}

bool BTableItem::setSelected(bool selected)
{
	if (member.table && member.selected != selected)
	{
		return member.table->setItemSelected(this, selected);
	}
	return false;
}
bool BTableItem::selected() const
{
	return member.table && member.selected;
}

void BTableItem::setChecked(bool checked)
{
	if (member.checked != checked)
	{
		member.checked = checked;
		emit(Signal_Checked, checked);
		this->fresh();
	}
}
bool BTableItem::checked() const
{
	return member.checked;
}

bool BTableItem::setForm(Form form)
{
	if (member.form != form)
	{
		member.form = form;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
Form BTableItem::form() const
{
	return member.form;
}

bool BTableItem::setAlign(Align align)
{
	if (member.align != align)
	{
		member.align = align;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
Align BTableItem::align() const
{
	return member.align;
}

bool BTableItem::setLineFeed(bool lineFeed)
{
	if (member.lineFeed != lineFeed)
	{
		member.lineFeed = lineFeed;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
bool BTableItem::lineFeed() const
{
	return member.lineFeed;
}

bool BTableItem::setColor(BByte r, BByte g, BByte b, BByte a)
{
	return setColor(BColor(r, g, b, a));
}
bool BTableItem::setColor(const BColor& color)
{
	if (member.color != color)
	{
		member.color = color;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
const BColor& BTableItem::color() const
{
	return member.color;
}

bool BTableItem::setGraph(Graph graph)
{
	if (member.graph != graph)
	{
		member.graph = graph;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
Graph BTableItem::graph() const
{
	return member.graph;
}

bool BTableItem::setImage(BImage* image)
{
	if (member.image != image)
	{
		member.image = image;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
BImage* BTableItem::image() const
{
	return member.image;
}

bool BTableItem::setIcon(BIcon* icon)
{
	if (member.icon != icon)
	{
		member.icon = icon;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
BIcon* BTableItem::icon() const
{
	return member.icon;
}

bool BTableItem::setIconSize(int width, int height)
{
	return setIconSize(BSize(width, height));
}
bool BTableItem::setIconSize(const BSize& iconSize)
{
	if (member.iconSize != iconSize)
	{
		member.iconSize = iconSize;
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
const BSize& BTableItem::iconSize() const
{
	return member.ia_iconSize;
}

bool BTableItem::setText(const BString& text)
{
	if (member.text != text)
	{
		member.text = text;
		member.block.clear();
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
const BString& BTableItem::text() const
{
	return member.text;
}

void BTableItem::setTips(const BString& tips)
{
	if (member.tips != tips)
	{
		member.tips = tips;
		object_member(this)->check_emit(Signal_TipsChanged, tips);
	}
}
const BString& BTableItem::tips() const
{
	return member.tips;
}

bool BTableItem::setWidget(BWidget* widget)
{
	if (member.widget != widget)
	{
		if (member.widget)
		{
			if (member.table)
				member.table->removeChild(member.widget);
			member.widget = 0;
		}
		member.widget = widget;
		if (member.widget)
		{
			if (member.table)
				member.table->addChild(member.widget);
			member.widget->setFloating(false);
		}
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
BWidget* BTableItem::widget()
{
	return member.widget;
}
const BWidget* BTableItem::widget() const
{
	return member.widget;
}

bool BTableItem::setTextFormat(BTextFormat* textFormat)
{
	if (member.block.format() != textFormat)
	{
		member.block.setFormat(textFormat);
		member.dirty = true;
		member.dirtyCell();
		return true;
	}
	return false;
}
BTextFormat* BTableItem::textFormat()
{
	return member.block.format();
}
const BTextFormat* BTableItem::textFormat() const
{
	return member.block.format();
}

void BTableItem::setBackground(BByte r, BByte g, BByte b, BByte a)
{
	setBackground(BColor(r, g, b, a));
}
void BTableItem::setBackground(const BColor& background)
{
	if (member.background != background)
	{
		member.background = background;
		if (member.table)
			member.table->fresh();
	}
}
const BColor& BTableItem::background() const
{
	return member.background;
}

void BTableItem::setForeground(BByte r, BByte g, BByte b, BByte a)
{
	setForeground(BColor(r, g, b, a));
}
void BTableItem::setForeground(const BColor& foreground)
{
	if (member.foreground != foreground)
	{
		member.foreground = foreground;
		if (member.table)
			member.table->fresh();
	}
}
const BColor& BTableItem::foreground() const
{
	return member.foreground;
}

int BTableItem::row() const
{
	if (member.table)
	{
		return member.row;
	}
	return -1;
}
int BTableItem::column() const
{
	if (member.table)
	{
		return member.column;
	}
	return -1;
}

bool BTableItem::hovered() const
{
	if (BTableWidget* table = member.table)
	{
		if (BTableItem* hoveredItem = table_member(table)->hoveredItem)
		{
			if (table_member(table)->selectMode & Select_Rows)
				return hoveredItem->row() == member.row;
			return hoveredItem == this;
		}
	}
	return false;
}
bool BTableItem::fresh() const
{
	if (BTableWidget* table = member.table)
	{
		table->fresh();
		return true;
	}
	return false;
}

BRect BTableItem::rect() const
{
	return BRect(member.pos, member.size);
}
BRect BTableItem::iconRect() const
{
	return member.ia_iconRect + member.pos;
}
BRect BTableItem::textRect() const
{
	return member.ia_textRect + member.pos;
}
BRect BTableItem::textArea() const
{
	return member.ia_textArea + member.pos;
}

BTableWidget* BTableItem::tableWidget()
{
	return member.table;
}
const BTableWidget* BTableItem::tableWidget() const
{
	return member.table;
}

